﻿Imports org.codegen.win.controls

Public Class frmCriteria

    Private _report As ReportMatadata

    Public Sub setReportMetadata(ByVal v As ReportMatadata)

        Me._report = v
        Me.Text = "Select Criteria for :" & Me._report.DisplayName
        Me.renderControls()
        Me.KeyPreview = True

    End Sub

    Private Sub renderControls()

        Dim lst As List(Of CriterionMetadata) = Me._report.Criteria
        If lst Is Nothing Then Exit Sub

        Dim con As New CriterionControlConverter
        Dim rowidx As Integer = -1
        Me.tblLayoutMain.CellBorderStyle = TableLayoutPanelCellBorderStyle.None

        For Each c As CriterionMetadata In lst

            Dim contrArray As Control() = con.convert(c).ToArray

            If c.isHidden Then
                If contrArray.Length > 0 Then
                    Me.Controls.Add(contrArray(0))
                End If

                If contrArray.Length > 1 Then
                    Me.Controls.Add(contrArray(1))
                End If

            Else

                If contrArray IsNot Nothing AndAlso contrArray.Length >= 2 Then
                    rowidx = rowidx + 1
                    Me.setErrorProvider(contrArray)
                    Me.tblLayoutMain.Controls.Add(contrArray(0), 0, rowidx)
                    Me.tblLayoutMain.Controls.Add(contrArray(1), 1, rowidx)

                    If String.IsNullOrEmpty(c.defaultValue) = False Then
                        'we need to set the default value here, we do not 
                        'have any items in combo boxes before call to  Me.tblLayoutMain.Controls.Add
                        CType(contrArray(1), ICGBaseControl).Value = c.defaultValue
                    End If

                    If contrArray.Length >= 4 Then
                        rowidx = rowidx + 1
                        Me.tblLayoutMain.Controls.Add(contrArray(2), 0, rowidx)
                        Me.tblLayoutMain.Controls.Add(contrArray(3), 1, rowidx)

                        If String.IsNullOrEmpty(c.defaultValue2) = False Then
                            'we need to set the default value here, we do not 
                            'have any items in combo boxes before call to  Me.tblLayoutMain.Controls.Add
                            CType(contrArray(3), ICGBaseControl).Value = c.defaultValue2
                        End If

                    End If

                    If String.IsNullOrEmpty(c.helpText) = False Then
                        rowidx = rowidx + 1
                        Dim helpLabel As Label = New Label
                        helpLabel.Text = c.helpText
                        helpLabel.Visible = True

                        helpLabel.TextAlign = ContentAlignment.TopLeft
                        helpLabel.Name = c.Name & "helptxt"
                        helpLabel.ForeColor = Color.Red

                        helpLabel.Location = New System.Drawing.Point(0, 0)
                        helpLabel.AutoSize = True ' = DockStyle.Fill
                        Me.tblLayoutMain.Controls.Add(helpLabel, 1, rowidx)
                        Me.tblLayoutMain.SetColumnSpan(helpLabel, 3)

                    End If

                End If
            End If
        Next

    End Sub

    Private Sub setErrorProvider(ByVal contrArray As Control())

        For Each c As Control In contrArray
            If TypeOf c Is ICGBaseControl Then
                CType(c, ICGBaseControl).ErrProvider = Me.ErrProvider
            End If
        Next

    End Sub

    ''' <summary>
    ''' loops thru each criterion and sets the value from the form
    ''' </summary>
    ''' <remarks></remarks>
    Private Sub setCriteriaValues()

        Dim lst As List(Of CriterionMetadata) = Me._report.Criteria
        For Each c As CriterionMetadata In lst

            Dim uicontrol() As Control = Me.Controls.Find(c.Name, True)
            If (uicontrol Is Nothing OrElse uicontrol.Length = 0) Then
                Throw New ApplicationException("Cannot find control with name: " & c.Name)
            End If

            Dim lUicontrol As Control = uicontrol(0)
            c.selectedvalue = c.getUIValue(lUicontrol)
            c.selectedDisplayValue = c.getUIDisplayValue(lUicontrol)

            Trace.TraceInformation("Set " & c.Name & " value to " & CStr(c.selectedvalue))

            If c.isRange Then
                uicontrol = Me.Controls.Find(c.Name2, True)
                If (uicontrol Is Nothing) Then
                    Throw New ApplicationException("Cannot find control with name: " & c.Name2)
                End If
                Dim lUicontrol2 As Control = uicontrol(0)
                c.selectedvalue2 = c.getUIValue(lUicontrol2)
                c.selectedDisplayValue2 = c.getUIDisplayValue(lUicontrol2)
                Trace.TraceInformation("Set " & c.Name2 & " value to " & CStr(c.selectedvalue2))
            End If

        Next

    End Sub

    Private Sub btnOK_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles cmdPrint.Click

        Me.ValidateAndPrintreport()

    End Sub

    Private Sub ValidateAndPrintreport()
        'validateAndPrintreport
        If Me.ValidateChildren() Then
            'start the mouse pointer
            winUtils.HourglassOn()
            Call setCriteriaValues()
            Me.DialogResult = DialogResult.OK

        End If
    End Sub
    Private Sub btnCancel_Click(ByVal sender As System.Object, _
                             ByVal e As System.EventArgs) Handles cmdCancel.Click

        Me.DialogResult = DialogResult.Cancel

    End Sub


    Private Sub frmCriteria_KeyDown(ByVal sender As Object, _
                                    ByVal e As System.Windows.Forms.KeyEventArgs) Handles Me.KeyDown


        If e.KeyCode = Keys.Escape Then
            Me.DialogResult = DialogResult.Cancel
            e.Handled = True

        ElseIf e.KeyCode = Keys.Enter Then
            Me.ValidateAndPrintreport()
            e.Handled = True

        End If


    End Sub
End Class